// This file is required by the index.html file and will
// be executed in the renderer process for that window.
// No Node.js APIs are available in this process because
// `nodeIntegration` is turned off. Use `preload.js` to
// selectively enable features needed in the rendering
// process.

const { ipcRenderer, remote } = require("electron")
const Store = require('electron-store');
const store = new Store({encryptionKey: 'ljsdfkl23nsf@#12'});

function refrushAccountList(){
  const storeAccount = store.get('v1.accounts')
  const selHtml = []
  
  storeAccount.forEach( item=>{
    selHtml.push(`<option value="${item.email}">${item.name}(${item.email})</option>`)
  })
  $("#account_sel").html(selHtml.join(''))
}

(function(){
  refrushAccountList()

  const admin_email = store.get('v1.admin_email')
  $("#set_email_account").val(admin_email.user)
//  $("#set_email_pwd").val(admin_email.password)
  $("#set_email_host").val(admin_email.host)
  $("#set_email_port").val(admin_email.port)
  $("#set_email_ssl").val(admin_email.tls ? '是' : '否')
})()

$("#set_admin_pwd_btn").click(function(){
  const pwd = $("#set_admin_pwd").val()
  if(pwd === '') return;

  $.confirm({
    title: '提示',
    content: `你是否确认修改管理密码?`,
    type: 'green',
    buttons: {
      omg: {
          text: '确定',
          btnClass: 'btn-green',
          action: function(){
            store.set('v1.admin_pwd', pwd)
          }
      },
      close: {
          text: '取消',
      }
    }
  })
})

$("#set_save_btn").click(function(){
  const user = $("#set_email_account").val()
  const password = $("#set_admin_pwd").val()
  const host = $("#set_email_host").val()
  const port = $("#set_email_port").val()
  const tls = $("#set_email_ssl").val() === '是'

  if(user === '' || password==='' || host==='' || port==='') return;

  $.confirm({
    title: '提示',
    content: `你是否保存邮箱账号信息?`,
    type: 'green',
    buttons: {
      omg: {
          text: '确定',
          btnClass: 'btn-green',
          action: function(){
            store.set('v1.admin_email', {user, password, host, port, tls})
          }
      },
      close: {
          text: '取消',
      }
    }
  })
})

$("#collapseThree").on('show.bs.collapse', function(e){
  $("#collapseOne, #collapseTwo").collapse('hide')
})

$("#collapseOne, #collapseTwo").on('show.bs.collapse', function(e){
  const _this = $(this)
  $.confirm({
    title: '校验管理密码',
    content: `<form action="" class="formName">
      <div class="form-group">
        <label>管理密码:</label>
        <input type="text" placeholder="管理密码" class="admin-pwd form-control" required/>
      </div>
    </form>`,
    buttons: {
      formSubmit: {
          text: '校验',
          btnClass: 'btn-blue',
          action: function () {
            const pwd = this.$content.find('.admin-pwd').val();
            if(pwd !== store.get('v1.admin_pwd')){
              $.alert('管理密码不正确！');
              // 阻止默认操作
              _this.collapse('hide')
              $("#collapseThree").collapse('show')
            }else {
              _this.collapse('show')
            }
          }
      },
      cancel: {
          text: '取消',
          action: function(){
            _this.collapse('hide')
            $("#collapseThree").collapse('show')
          }
      },
    },
    onContentReady: function () {
        var jc = this;
        this.$content.find('form').on('submit', function (e) {
            e.preventDefault();
            jc.$$formSubmit.trigger('click');
        });
    }
  });

})
